<%--
    Страница групп товаров.

    Содержит дерево групп товаров, позволяет добавлять, удалять и редактировать группы товаров.

--%>
<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Goodwill - Группы товаров</title>
    <link rel="stylesheet" href="resources/css/main.css"/>
    <link rel="stylesheet" href="resources/css/stylesheet.css"/>

    <script type="text/javascript">

        //  Сохранить привызку группы товаров к брэнду
        function saveGoodTypeBrand(goodTypeId, brandId)
        {
            _saveGoodTypeBrand(goodTypeId, brandId);
        }

    </script>

    <style type="text/css">
        .gt_img
        {
            margin-right: 5px;
        }
    </style>
</head>
<body>
<f:view>
<jsp:include page="menu.jsp"/>
<jsp:include page="status.jsp"/>

<a4j:form id="jsForm" prependId="false">
    <a4j:jsFunction name="_saveGoodTypeBrand" action="#{GoodTypesBean.saveGoodTypeBrand}">
        <a4j:actionparam name="goodTypeId" assignTo="#{GoodTypesBean.params.goodTypeId}"/>
        <a4j:actionparam name="brandId" assignTo="#{GoodTypesBean.params.brandId}"/>
    </a4j:jsFunction>
</a4j:form>

<h:form>
        <rich:panel header="Группы товаров" id="panelGroups">

            <h:inputHidden value="#{GoodTypesBean.showHidden}"/>
            <h:panelGroup rendered="#{SecurityBean.isRoleAdministrator}">
                <a4j:commandLink reRender="panelGroups" action="#{GoodTypesBean.invertShowHidden}">
                    <h:graphicImage value="/resources/img/checkbox_checked.gif" rendered="#{GoodTypesBean.showHidden}"/>
                    <h:graphicImage value="/resources/img/checkbox_unchecked.gif" rendered="#{!GoodTypesBean.showHidden}"/>
                </a4j:commandLink>
                <h:outputText value="Показать все"/>
            </h:panelGroup>

            <%-- Дерево групп товаров --%>
            <br><br>
            <a4j:repeat value="#{GoodTypesBean.goodTypes}" var="goodType">
                <rich:spacer width="#{(goodType.level)*20}"/>
                <h:graphicImage value="/resources/img/folder.gif" styleClass="gt_img"/>

                <%-- Наименование группы товаров--%>
                <a4j:commandLink action="#" ajaxSingle="true" styleClass="gt_img"
                                 oncomplete="document.location='GoodsPage.jsf'"
                                 id="lGoodType">
                    <h:outputText value="#{goodType.name}" rendered="#{goodType.deleted}" styleClass="goodTypeDeleted"/>
                    <h:outputText value="#{goodType.name}" rendered="#{!goodType.deleted}" styleClass="goodType"/>
                    <a4j:actionparam name="good_code" assignTo="#{SessionBean.goodsFilter.code}" value=""/>
                    <a4j:actionparam name="good_name" assignTo="#{SessionBean.goodsFilter.name}" value=""/>
                    <a4j:actionparam name="good_type" assignTo="#{SessionBean.goodsFilter.goodTypeId}" value="#{goodType.id}"/>
                </a4j:commandLink>

                <h:panelGroup rendered="#{SecurityBean.isRoleAdministrator}">

                    <h:selectOneMenu value="#{goodType.brandId}" rendered="#{goodType.level == 1}" style="width:100px" onchange="saveGoodTypeBrand(#{goodType.id}, this.value);">
                        <f:selectItems value="#{BrandsBean.brandsAsSelectItems}"/>
                    </h:selectOneMenu>

                <%-- Создать дочернуюю группу --%>
                <a4j:commandLink ajaxSingle="true"
                                 action="#"
                                 oncomplete="#{rich:component('modalGroup')}.show()"
                                 reRender="modalGroup"
                                 id="lCreateGoodType">
                    <h:graphicImage value="/resources/img/document_add.png"/>
                    <a4j:actionparam name="group_id" assignTo="#{GoodTypesBean.goodType.id}" value="#{-1}"/>
                    <a4j:actionparam name="parent_id" assignTo="#{GoodTypesBean.goodType.parentId}" value="#{goodType.id}"/>
                    <a4j:actionparam name="group_name" assignTo="#{GoodTypesBean.goodType.name}" value=""/>
                    <a4j:actionparam name="parent_name" assignTo="#{GoodTypesBean.parentName}"
                                     value="#{goodType.name}"/>
                </a4j:commandLink>
                <rich:toolTip for="lCreateGoodType">Создать дочернюю группу товаров</rich:toolTip>

                </h:panelGroup>
                <h:panelGroup rendered="#{goodType.level != 0 && SecurityBean.isRoleAdministrator}">

                <%-- Редактировать группу --%>
                <a4j:commandLink ajaxSingle="true"
                                 action="#"
                                 oncomplete="#{rich:component('modalGroup')}.show()"
                                 reRender="modalGroup"
                                 rendered="#{goodType.id != 0}"
                                 id="lEditGoodType">
                    <h:graphicImage value="/resources/img/edit.gif"/>
                    <a4j:actionparam name="group_id" assignTo="#{GoodTypesBean.goodType.id}" value="#{goodType.id}"/>
                    <a4j:actionparam name="parent_id" assignTo="#{GoodTypesBean.goodType.parentId}" value="#{goodType.parentId}"/>
                    <a4j:actionparam name="group_name" assignTo="#{GoodTypesBean.goodType.name}" value="#{goodType.name}"/>
                    <a4j:actionparam name="parent_name" assignTo="#{GoodTypesBean.parentName}" value="#{goodType.parent.name}"/>
                </a4j:commandLink>
                <rich:toolTip for="lEditGoodType">Редактировать наименование группы товаров</rich:toolTip>

                <%-- Удалить группу --%>
                <a4j:commandLink ajaxSingle="true"
                                 action="#"
                                 oncomplete="#{rich:component('modalDeleteGroup')}.show()"
                                 reRender="modalDeleteGroup"
                                 rendered="#{goodType.id != 0}"
                                 id="lDeleteGoodType">
                    <h:graphicImage value="/resources/img/delete.gif"/>
                    <a4j:actionparam name="group_id" assignTo="#{GoodTypesBean.goodType.id}" value="#{goodType.id}"/>
                    <a4j:actionparam name="group_name" assignTo="#{GoodTypesBean.goodType.name}" value="#{goodType.name}"/>
                    <a4j:actionparam name="parent_name" assignTo="#{GoodTypesBean.parentName}"
                                     value="#{goodType.parent.name}"/>
                </a4j:commandLink>
                <rich:toolTip for="lDeleteGoodType">Удалить группу товаров</rich:toolTip>

                <%-- Переместить группу --%>
                <a4j:commandLink ajaxSingle="true"
                                 action="#"
                                 oncomplete="#{rich:component('modalMoveGroup')}.show()"
                                 reRender="modalMoveGroup"
                                 rendered="#{goodType.id != 0}"
                                 id="lMoveGoodType">
                    <h:graphicImage value="/resources/img/copy.png"/>
                    <a4j:actionparam name="group_id" assignTo="#{GoodTypesBean.goodType.id}" value="#{goodType.id}"/>
                    <a4j:actionparam name="group_name" assignTo="#{GoodTypesBean.goodType.name}" value="#{goodType.name}"/>
                    <a4j:actionparam name="parent_name" assignTo="#{GoodTypesBean.parentName}" value="#{goodType.parent.name}"/>
                </a4j:commandLink>
                <rich:toolTip for="lMoveGoodType">Переместить группу и товары, входящие в нее</rich:toolTip>
                </h:panelGroup>
                <br>
            </a4j:repeat>

        </rich:panel>
    </h:form>

    <rich:modalPanel id="modalGroup" autosized="true" width="550">
        <f:facet name="header">
            <h:panelGroup>
                <h:outputText value="Создание/редактирование дочерней группы"/>
            </h:panelGroup>
        </f:facet>
        <f:facet name="controls">
            <h:graphicImage value="/resources/img/close.png" styleClass="hidelink"
                            onclick="#{rich:component('modalGroup')}.hide();return false;"/>
        </f:facet>
        <a4j:outputPanel ajaxRendered="false">
            <h:form>

                <h:inputHidden value="#{GoodTypesBean.goodType.id}"/>
                <h:inputHidden value="#{GoodTypesBean.goodType.parentId}"/>

                <jsp:include page="messages.jsp"/>

                <h:panelGrid columns="2" styleClass="full" columnClasses="dleft,dright" cellpadding="0" cellspacing="0">
                    <h:outputText value="Родительская группа:"/>
                    <h:outputText value="#{GoodTypesBean.parentName}"/>
                    <h:outputText value="Название:"/>
                    <h:inputText value="#{GoodTypesBean.goodType.name}"
                                 required="true"
                                 requiredMessage="Поле Название обязательно для заполнения"/>
                </h:panelGrid>

                <h:panelGrid columns="2" styleClass="full" columnClasses="butLeft,butRight" cellpadding="0"
                             cellspacing="0">
                    <a4j:commandButton value="Сохранить"
                                       action="#{GoodTypesBean.saveGoodType}"
                                       reRender="panelGroups"
                                       styleClass="btnsz"
                                       oncomplete="if(#{!GoodTypesBean.hasFacesErrors}) #{rich:component('modalGroup')}.hide();"/>
                    <a4j:commandButton value="Отмена"
                                       id="bCancelCreateSubGroup"
                                       styleClass="btnsz"
                                       onclick="#{rich:component('modalGroup')}.hide();return false;"/>
                </h:panelGrid>
            </h:form>
        </a4j:outputPanel>
    </rich:modalPanel>

    <rich:modalPanel id="modalDeleteGroup" autosized="true" width="300">
        <f:facet name="header">
            <h:panelGroup>
                <h:outputText value="Внимание!"/>
            </h:panelGroup>
        </f:facet>
        <f:facet name="controls">
            <h:graphicImage value="/resources/img/close.png" styleClass="hidelink"
                            onclick="#{rich:component('modalDeleteGroup')}.hide();return false;"/>
        </f:facet>
        <a4j:outputPanel ajaxRendered="false">
            <h:form>

                <jsp:include page="messages.jsp"/>

                <h:inputHidden value="#{GoodTypesBean.goodType.id}"/>

                <h:panelGrid columns="2" styleClass="full" cellpadding="0" cellspacing="0">
                    <h:graphicImage value="/resources/img/warning_32.png"/>
                    <h:outputText value="Вы хотите удалить группу \"#{GoodTypesBean.goodType.name}\"?"/>
                </h:panelGrid>

                <h:panelGrid columns="2" styleClass="full" columnClasses="butLeft,butRight" cellpadding="0"
                             cellspacing="0">
                    <a4j:commandButton value="Да"
                                       action="#{GoodTypesBean.deleteGoodType}"
                                       styleClass="btnsz"
                                       reRender="panelGroups"
                                       oncomplete="if(#{!GoodTypesBean.hasFacesErrors}) #{rich:component('modalDeleteGroup')}.hide();"/>
                    <a4j:commandButton value="Нет"
                                       styleClass="btnsz"
                                       oncomplete="#{rich:component('modalDeleteGroup')}.hide();return false;"/>
                </h:panelGrid>
            </h:form>
        </a4j:outputPanel>
    </rich:modalPanel>

    <rich:modalPanel id="modalMoveGroup" autosized="true" width="300">
        <f:facet name="header">
            <h:panelGroup>
                <h:outputText value="Перемещение группы"/>
            </h:panelGroup>
        </f:facet>
        <f:facet name="controls">
            <h:graphicImage value="/resources/img/close.png" styleClass="hidelink" onclick="#{rich:component('modalMoveGroup')}.hide();return false;"/>
        </f:facet>
        <a4j:outputPanel ajaxRendered="false">
            <h:form>
                <h:inputHidden value="#{GoodTypesBean.goodType.id}"/>

                <jsp:include page="messages.jsp"/>

                <h:outputText value="В какую группу вы хотите переместить группу \"#{GoodTypesBean.goodType.name}\"?"/>

                <rich:panel>
                    <a4j:repeat value="#{GoodTypesBean.goodTypes}" var="goodType">
                        <rich:spacer width="#{(goodType.level)*20}"/>
                        <h:graphicImage value="/resources/img/folder.gif" styleClass="gt_img"/>

                        <a4j:commandLink action="#{GoodTypesBean.moveGoodType}"
                                         value="#{goodType.name}"
                                         oncomplete="if(#{!GoodTypesBean.hasFacesErrors}) #{rich:component('modalMoveGroup')}.hide();"
                                         reRender="panelGroups">
                            <a4j:actionparam name="good_type_from" assignTo="#{GoodTypesBean.goodTypeIdFrom}" value="#{GoodTypesBean.goodType.id}"/>
                            <a4j:actionparam name="good_type_to" assignTo="#{GoodTypesBean.goodTypeIdTo}" value="#{goodType.id}"/>
                        </a4j:commandLink><br>

                    </a4j:repeat>
                </rich:panel>
            </h:form>
        </a4j:outputPanel>
    </rich:modalPanel>

</f:view>
</body>
</html>
